package com.huifaapp.mall.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.DetailsofPaymentActivity;
import com.huifaapp.mall.adapter.AllOrderListAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.AllorderBean;
import com.huifaapp.mall.bean.event.OrderStatusEventMessage;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class AllOrdersFragment extends Fragment {


    private RecyclerView orderRecy;
    private List<AllorderBean.OrderListBean> lists;
    private AllOrderListAdapter allOrderListAdapter;

    public AllOrdersFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_all_orders,null);
        initView(view);
        orderRecy.setLayoutManager(new LinearLayoutManager(getActivity()));
        EventBus.getDefault().register(this);
        return view;
    }

    @Subscribe
    public void onEvent(OrderStatusEventMessage orderStatusEventMessage){
        Log.d("NIHAOA", "onEvent: " + orderStatusEventMessage.toString());
    }



    private boolean isGetData = false;
    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        //   进入当前Fragment
        if (enter && !isGetData) {
            isGetData = true;
            //   这里可以做网络请求或者需要的数据刷新操作
            OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/myorders"+"?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+ AUniqueIdentifier.getDeviceId(getActivity()))
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {

                        }

                        @Override
                        public void onResponse(String response) {

                            Log.i("pendingHB",response);
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                JSONArray orderList = jsonObject.getJSONArray("orderList");
                                Log.e("FH",orderList+"");
                                initData(response);
                            } catch (JSONException e) {
                                Log.e("FH","HHH");
                            }

                        }


                    });
        } else {
            isGetData = false;
        }
        return super.onCreateAnimation(transit, enter, nextAnim);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        isGetData = false;
        super.onDestroy();
    }
    @Override
    public void onResume() {
        super.onResume();
        if (!isGetData) {
            //   这里可以做网络请求或者需要的数据刷新操作
            OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/myorders"+"?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+ AUniqueIdentifier.getDeviceId(getActivity()))
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {

                        }

                        @Override
                        public void onResponse(String response) {

                            Log.i("pendingHB",response);
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                JSONArray orderList = jsonObject.getJSONArray("orderList");
                                Log.e("FH",orderList+"");
                                initData(response);
                            } catch (JSONException e) {
                                Log.e("FH","HHH");
                            }

                        }


                    });
            isGetData = true;
        }
    }

    private void initView(View view) {
        orderRecy = (RecyclerView) view.findViewById(R.id.allorderrecy);
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/myorders"+"?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+ AUniqueIdentifier.getDeviceId(getActivity()))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {

                        Log.i("pendingHB",response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONArray orderList = jsonObject.getJSONArray("orderList");
                            Log.e("FH",orderList+"");
                            initData(response);
                        } catch (JSONException e) {
                            Log.e("FH","HHH");
                        }
                    }
                });
    }

    private void initData(String response) {
        final Gson gson = new Gson();
        AllorderBean allorderBean = gson.fromJson(response,AllorderBean.class);
        lists = allorderBean.getOrderList();
        allOrderListAdapter = new AllOrderListAdapter(getActivity(),lists);
        orderRecy.setAdapter(allOrderListAdapter);
    }


}
